package com.hqizcc.ltcs.hqbsltcs.service;

import com.hqizcc.ltcs.hqbsltcs.common.Global;
import com.hqizcc.ltcs.hqbsltcs.mapper.GetCountyMapper;
import com.hqizcc.ltcs.hqbsltcs.mapper.GetTownMapper;
import com.hqizcc.ltcs.hqbsltcs.model.County;
import com.hqizcc.ltcs.hqbsltcs.model.Town;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class GetTownServiceImpl implements GetTownService {

    @Autowired
    private GetCountyMapper getCountyMapper;

    @Autowired
    private GetTownMapper getTownMapper;

    @Override
    public boolean getTown() {

        List<County>  countyList = getCountyMapper.findAllCounty();

        System.out.println(countyList.toString());

        Town town = new Town();

        int count  = 0 ;

        for (County county : countyList){

            String countrUrl = county.getCountyHtml();

            try {

                if ("没有下属的地区".equals(countrUrl)){
                    continue;
                }

                Element body = Jsoup.connect(countrUrl).ignoreContentType(true).timeout(600000).get().body();

                Elements elements = body.select(".towntr");

                for (Element element : elements){

                    Elements td = element.select("td");

                    Elements a = td.select("a");

                    if (a.isEmpty()){
                        String countyIdAndName = td.text();

                        String townId = countyIdAndName.substring(0,12);

                        String townName = new String(countyIdAndName.substring(13).getBytes(), "UTF-8");

                        System.out.println(townId + " " + townName);

                        String townUrl = "没有下属的地区";

//                        System.out.println(countyId + " " + countyName + " " +  countrUrl);

                       town.setTownId(townId);
                       town.setTownName(townName);
                       town.setTownHtml(townUrl);


                        String county_id = getTownMapper.getCountyAndTownByTownId(town.getTownId());

//                        System.out.println(county_id);

                        String town_name = getTownMapper.getTownByTownId(town.getTownId());

//                        System.out.println(town_name);

                        System.out.println(town.getTownId() + town.getTownName() +  town.getTownHtml());

                        if ( county_id == null ){

                            getTownMapper.saveCountyAndTown(county.getCountyId(), town.getTownId());

                        } else {

                            System.out.println("数据库中已存在");

                        }

                        if ( town_name == null ){

                            getTownMapper.saveTown(town.getTownId(), town.getTownName(), town.getTownHtml());

                        } else {

                            System.out.println("数据库已存在");

                        }


                        System.out.println(count ++ );

                        Thread.sleep(1000*5);

                    } else {

                        String townUrl = td.select("a").attr("href");

                        String countyIdAndName = td.select("a").text();

//                        System.out.println(countyIdAndName);

                        String townId = countyIdAndName.substring(0, 12);

                        String townName = new String(countyIdAndName.substring(13).getBytes(), "UTF-8");

                        String url = townUrl.substring(townUrl.indexOf("/")+1);

                        townUrl = Global.URL + url.substring(0,2) + "/" + url.substring(2,4) + "/" + townUrl;

//                        System.out.println(countyId + " " + countyName + " " + countyUrl);

                        town.setTownId(townId);
                        town.setTownName(townName);
                        town.setTownHtml(townUrl);

                        String county_id = getTownMapper.getCountyAndTownByTownId(town.getTownId());
//                        System.out.println(county_id);

                        String town_name = getTownMapper.getTownByTownId(town.getTownId());
//                        System.out.println(town_name);

                        System.out.println(town.getTownId() + town.getTownName() +  town.getTownHtml());

                        if ( county_id == null ){

                            getTownMapper.saveCountyAndTown(county.getCountyId(), town.getTownId());

                        } else {

                            System.out.println("数据库中已存在");

                        }

                        if ( town_name == null ){

                            getTownMapper.saveTown(town.getTownId(), town.getTownName(), town.getTownHtml());


                        } else {

                            System.out.println("数据库已存在");

                        }


                        System.out.println(count ++ );

                        Thread.sleep(1000*5);
                    }

                    break;


                }

                Thread.sleep(1000*5);

            } catch (IOException e) {

                e.printStackTrace();

                return false;
            } catch (InterruptedException e) {

                e.printStackTrace();

                return false;
            }
        }

        return true;

    }
}
